import axios from 'axios';
axios.defaults.baseURL = process.env.NODE_ENV == "development" ? 'http://39.105.131.187:8011/api/' : 'http://39.105.131.187:8011/api/'; // 判断是开发环境还是真是的环境
// 登录请求方法
const loginreq = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/json; charset=utf-8',
        },
        data: params,
        // traditional: true,
        // transformRequest: [
        //     function(data) {
        //         let ret = ''
        //         for (let it in data) {
        //             ret +=
        //                 encodeURIComponent(it) +
        //                 '=' +
        //                 encodeURIComponent(data[it]) +
        //                 '&'
        //         }
        //         return ret
        //     }
        // ]
    }).then(res => res.data);
};
// 通用公用方法
const req = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/json; charset=utf-8',
            token: localStorage.getItem('logintoken')
        },
        data: params,
    }).then(res => res.data);
};

// get请求
const get = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        params: params,
    }).then(res => res.data);
};
// downlaod 用来下载二进制文件流
const download = (method, url, params) => {
    return axios({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/json; charset=utf-8',
            token: localStorage.getItem('logintoken'),
        },
        responseType: 'arraybuffer',
        data: params,
    }).then(res => res.data);
};

export {
    loginreq,
    req,
    get,
    download
}